
package com.cai310.lottery.web.controller.admin.ticket;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;

import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.LotteryCategory;
import com.cai310.lottery.entity.lottery.ticket.LotterySupporter;
import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.entity.security.AdminUser;
import com.cai310.lottery.entity.user.User;
import com.cai310.lottery.service.QueryService;
import com.cai310.lottery.service.ServiceException;
import com.cai310.lottery.service.lottery.ticket.impl.LotterySupporterEntityManager;
import com.cai310.lottery.ticket.common.TicketQuery;
import com.cai310.lottery.ticket.common.TicketSupporter;
import com.cai310.lottery.web.controller.WebDataException;
import com.cai310.lottery.web.controller.admin.AdminBaseController;
import com.cai310.orm.Pagination;
import com.cai310.orm.XDetachedCriteria;
import com.cai310.utils.Struts2Utils;

public class TicketController extends AdminBaseController
{
    private static final long serialVersionUID = -7128890110025210382L;

    @Autowired
    private LotterySupporterEntityManager lotterySupporterEntityManager;

    @Autowired
    private QueryService queryService;

    private Long id;

    private Integer index;

    private Integer[] supporters = new Integer[Lottery.values().length];

    private List<LotterySupporter> lotterySupporterList = new ArrayList<LotterySupporter>();

    private Lottery lotteryType;//彩种

    private TicketSupporter ticketSupporter;//出票商

    private String periodNumber;//期号

    private String schemeNumber;//方案编号

    private String ticketId;//票号

    public String getTicketId()
    {
        return ticketId;
    }

    public void setTicketId(String ticketId)
    {
        this.ticketId = ticketId;
    }

    private String ticketState;//方案状态 1-成功、0-失败

    private Pagination pagination = new Pagination(20);

    private String titckForwardURL;//转向URL。供给/resources/struts.xml调用

    /**
     * 
     * @return
     */
    public String gotoScheme()
    {
        try
        {
            AdminUser adminUser = getAdminUser();
            if (null != adminUser)
            {
                ///后台用户
                if (StringUtils.isBlank(schemeNumber))
                    throw new WebDataException("方案号为空.");
                Lottery lottery = lotteryType.valueOfSchemeNumber(schemeNumber);
                if (lottery == null)
                    throw new ServiceException("方案类别不正确.");
                Long schemeId = lottery.getSchemeId(schemeNumber);
                if (schemeId == null)
                    throw new ServiceException("方案号不正确.");

                if (lottery.getCategory().equals(LotteryCategory.FREQUENT))
                {
                    //高频
                    titckForwardURL = "/" + lottery.getKey() + "/scheme!show.action?id=" + schemeId;
                }
                else
                {
                    titckForwardURL = "/admin/lottery/" + lottery.getKey() + "/scheme!show.action?schemeId=" + schemeId;
                }
                return "titckForwardURL";
            }
        }
        catch (WebDataException e)
        {
            addActionError(e.getMessage());
        }
        catch (ServiceException e)
        {
            addActionError(e.getMessage());
        }
        catch (Exception e)
        {
            addActionError(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return this.error();
    }

    /**
     * 获取出票设置信息
     * @return
     */
    public String ticketSupporter()
    {
        List<LotterySupporter> list = lotterySupporterEntityManager.findLotterySupporter();
        for (Lottery l : Lottery.values())
        {
            LotterySupporter lotterySupporter = new LotterySupporter();
            lotterySupporter.setLotteryType(l);
            for (LotterySupporter ls : list)
            {
                if (ls.getLotteryType() == l)
                {
                    lotterySupporter = ls;
                    break;
                }
            }
            lotterySupporterList.add(lotterySupporter);
        }
        return "ticket-supporter";
    }

    /**
     * 保存彩种出票设置
     * @return
     */
    public String saveLotterySupporter()
    {
        Integer supporterIndex = supporters[index];
        TicketSupporter ticketSupporter = TicketSupporter.values()[supporterIndex];
        Lottery lotteryType = Lottery.values()[index];
        LotterySupporter lotterySupporter = new LotterySupporter();
        lotterySupporter.setId(id);
        lotterySupporter.setTicketSupporter(ticketSupporter);
        lotterySupporter.setLotteryType(lotteryType);
        lotterySupporterEntityManager.saveLotterySupporter(lotterySupporter);
        return this.ticketSupporter();
    }

    /**
     * 修改出票状态
     * @return
     */
    public String updateTiketState()
    {
        LotterySupporter lotterySupporter = lotterySupporterEntityManager.getLotterySupporter(id);
        if (this.id == null)
            Struts2Utils.renderText("数据库中无此记录!");
        lotterySupporter.setUsable(!lotterySupporter.isUsable());
        lotterySupporterEntityManager.saveLotterySupporter(lotterySupporter);
        String stateString = lotterySupporter.isUsable() ? "开启中" : "关闭中";
        Struts2Utils.renderText(stateString);
        return null;
    }

    /**
     * 修改出票状态
     * @return
     */
    public String ticketList()
    {
        XDetachedCriteria criteria = new XDetachedCriteria(Ticket.class, "m");
        ProjectionList propList = Projections.projectionList();
        propList.add(Projections.property("id"), "id");
        propList.add(Projections.property("createTime"), "createTime");
        propList.add(Projections.property("schemeNumber"), "schemeNumber");
        propList.add(Projections.property("lotteryType"), "lotteryType");
        propList.add(Projections.property("periodNumber"), "periodNumber");
        propList.add(Projections.property("mode"), "mode");
        propList.add(Projections.property("schemeCost"), "schemeCost");
        propList.add(Projections.property("ticketFinsh"), "ticketFinsh");
        propList.add(Projections.property("stateCode"), "stateCode");
        propList.add(Projections.property("remoteTicketId"), "remoteTicketId");
        propList.add(Projections.property("sendTime"), "sendTime");
        propList.add(Projections.property("stateCodeMessage"), "stateCodeMessage");
        propList.add(Projections.property("stateModifyTime"), "stateModifyTime");
        propList.add(Projections.property("ticketSupporter"), "ticketSupporter");
        criteria.setProjection(propList);
        criteria.setResultTransformer(Transformers.aliasToBean(TicketForm.class));

        if (lotteryType != null)
        {
            criteria.add(Restrictions.eq("m.lotteryType", lotteryType));
        }
        if (ticketSupporter != null)
        {
            criteria.add(Restrictions.eq("m.ticketSupporter", ticketSupporter));
        }
        if (StringUtils.isNotBlank(periodNumber))
        {
            criteria.add(Restrictions.eq("m.periodNumber", periodNumber));
        }

        if (StringUtils.isNotBlank(ticketId))
        {
            criteria.add(Restrictions.eq("m.id", Long.valueOf(ticketId)));
        }

        if (StringUtils.isNotBlank(ticketState))
        {
            if ("1".equals(ticketState))
            {
                criteria.add(Restrictions.or(Restrictions.eq("m.stateCode", "1"), Restrictions.eq("m.stateCode", "0")));
            }
            else
            {
                criteria.add(Restrictions.or(Restrictions.isNull("m.stateCode"), Restrictions.gt("m.stateCode", "1")));
            }
        }
        if (StringUtils.isNotBlank(schemeNumber))
        {
            criteria.add(Restrictions.eq("m.schemeNumber", schemeNumber));
        }
        criteria.addOrder(Order.desc("m.id"));
        pagination = queryService.findByCriteriaAndPagination(criteria, pagination);
        return "list";
    }

    /**
     * @return the lotterySupporterList
     */
    public List<LotterySupporter> getLotterySupporterList()
    {
        return lotterySupporterList;
    }

    /**
     * @param lotterySupporterList the lotterySupporterList to set
     */
    public void setLotterySupporterList(List<LotterySupporter> lotterySupporterList)
    {
        this.lotterySupporterList = lotterySupporterList;
    }

    /**
     * @return the supporters
     */
    public Integer[] getSupporters()
    {
        return supporters;
    }

    /**
     * @param supporters the supporters to set
     */
    public void setSupporters(Integer[] supporters)
    {
        this.supporters = supporters;
    }

    /**
     * @return the id
     */
    public Long getId()
    {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id)
    {
        this.id = id;
    }

    /**
     * @param index the index to set
     */
    public void setIndex(Integer index)
    {
        this.index = index;
    }

    /**
     * @return the lotteryType
     */
    public Lottery getLotteryType()
    {
        return lotteryType;
    }

    /**
     * @param lottery the lottery to set
     */
    public void setLotteryType(Lottery lotteryType)
    {
        this.lotteryType = lotteryType;
    }

    /**
     * @return the ticketSupporter
     */
    public TicketSupporter getTicketSupporter()
    {
        return ticketSupporter;
    }

    /**
     * @param ticketSupporter the ticketSupporter to set
     */
    public void setTicketSupporter(TicketSupporter ticketSupporter)
    {
        this.ticketSupporter = ticketSupporter;
    }

    /**
     * @return the periodNumber
     */
    public String getPeriodNumber()
    {
        return periodNumber;
    }

    /**
     * @param periodNumber the periodNumber to set
     */
    public void setPeriodNumber(String periodNumber)
    {
        this.periodNumber = periodNumber;
    }

    /**
     * @return the schemeNumber
     */
    public String getSchemeNumber()
    {
        return schemeNumber;
    }

    /**
     * @param schemeNumber the schemeNumber to set
     */
    public void setSchemeNumber(String schemeNumber)
    {
        this.schemeNumber = schemeNumber;
    }

    /**
     * @return the ticketState
     */
    public String getTicketState()
    {
        return ticketState;
    }

    /**
     * @param ticketState the ticketState to set
     */
    public void setTicketState(String ticketState)
    {
        this.ticketState = ticketState;
    }

    /**
     * @return the pagination
     */
    public Pagination getPagination()
    {
        return pagination;
    }

    /**
     * @param pagination the pagination to set
     */
    public void setPagination(Pagination pagination)
    {
        this.pagination = pagination;
    }

    public String getTitckForwardURL()
    {
        return titckForwardURL;
    }

    public void setTitckForwardURL(String titckForwardURL)
    {
        this.titckForwardURL = titckForwardURL;
    }

}
